<?php

class log_m_core extends a_base_model {

    public function __construct() {
        parent::__construct(FALSE);
    }

    public function insert($module, $msg,$class, $file, $func, $line, $type, $group="") {
        $this->db()
                ->set("module_name", $module)
                ->set("message", $msg)
                ->set("group", $group)
                ->set("file", $file)
                ->set("class", $class)
                ->set("function", $func)
                ->set("line", $line)
                ->set("type", $type)
                ->insert("log");
    }


    public function get_data($start,$num_of_records,$criteria=array(),$orders=array('id'=>'DESC')){
        $this->__process_criteria($criteria);
        if(count($orders)>0){
            foreach ($orders as $key => $val) {
                $this->db()->order_by($key, $val);
            }
        }
        $query = $this->db()->limit($num_of_records,$start)->get("log");

        $list = array();
        if($query->num_rows()>0){            
            foreach($query->result_array() as $row){
                $list[] = array_values($row);//self::build_object($d);
            }
        }

        return $list;

    }

    public function count_rows($criteria=array()){
        $this->__process_criteria($criteria);
        $query = $this->db()->get("log");
        return $query->num_rows();
        

    }

    public function delete_log($criteria=array()){
        $this->__process_criteria($criteria);
        if(count($criteria)>0){
            $this->db()->delete("log");
        }else{
            $this->db()->query("DELETE FROM log");
            $this->db()->query("ALTER TABLE log AUTO_INCREMENT=1");            
        }
        
        return $this->db()->affected_rows();
    }

    private function __process_criteria($criteria=array()){
          if (count($criteria) > 0) {
            foreach ($criteria as $index=>$condition) {
                foreach ($condition as $key => $val) {
                    if($index=="where"){
                       $this->db()->where($key,$val);
                    }elseif($index=='like'){
                        $this->db()->like($key,$val);
                    }
                }
            }
        }
    }



}

?>